const { Controller } = require("egg");
class GoodsController extends Controller {
  async addGoods() {
    // 新增商品
    const { ctx, service } = this;
    ctx.validate({
      categoryId: { type: "nullValue", fieldName: "商品分类" },
      image: { type: "nullValue", fieldName: "商品图片" },
      name: { type: "nullValue", fieldName: "商品名称" },
      description: { type: "nullValue", fieldName: "商品描述" },
      price: { type: "nullValue", fieldName: "商品价格" },
    });
    const res = await service.goods.addGoods(ctx.request.body);
    ctx.send(res);
  }
  // 获取商品列表
  async getGoodsList() {
    const { ctx, service } = this;
    const { page, size } = ctx.query;
    const res = await service.goods.getGoodsList(page, size);
    ctx.send(res);
  }
  // 删除商品
  async delGoods() {
    const { ctx, service } = this;
    const { _id } = ctx.params;
    const res = await service.goods.delGoods(_id);
    ctx.send(res);
  }
  // 获取商品详情
  async getGoodsDetail() {
    const { ctx } = this;
    const { _id } = ctx.query;
    const res = await ctx.model.Goods.findById(_id);
    ctx.send({ msg: "成功", data: res });
  }
  // 模糊查询商品
  async searchGoodsList() {
    const { ctx } = this;
    const { keyword } = ctx.query;
    const res = await ctx.service.goods.searchGoodsList(keyword);
    ctx.send(res);
  }
}
module.exports = GoodsController;
